package com.jike.noobmoney.mvp.view.activity;

import android.content.Intent;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.RegistEntity;
import com.jike.noobmoney.entity.SmsEntity;
import com.jike.noobmoney.net.RetrofitHelper;
import com.jike.noobmoney.util.PerfectClickListener;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.ToastUtils;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EditPasswordActivity extends BaseActivity {


    @BindView(R.id.img_logo)
    ImageView imgLogo;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.cb_pwd)
    CheckBox cbPwd;
    @BindView(R.id.et_password_confirm)
    EditText etPasswordConfirm;
    @BindView(R.id.cb_pwd_confirm)
    CheckBox cbPwdConfirm;

    @BindView(R.id.et_old_password)
    EditText etPasswordOld;

    @BindView(R.id.cb_old_pwd)
    CheckBox cbPwdOld;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.ll_register)
    LinearLayout llRegister;

    private boolean timeEnable = true;
    private Call<SmsEntity> codeCall;
    private String smscode;
    private Call<RegistEntity> registCall;
    public String   userid ;

    @Override
    protected int layoutResID() {
        return R.layout.activity_password_edit;
    }

    @Override
    protected void initData() {
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        setListener();
    }

    /**
     * 设置监听
     */
    private void setListener() {

        //旧密码
        cbPwdOld.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (!isChecked) {
                etPasswordOld.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                etPasswordOld.setSelection(etPasswordOld.getText().length());
            } else {
                etPasswordOld.setTransformationMethod(PasswordTransformationMethod.getInstance());
                etPasswordOld.setSelection(etPasswordOld.getText().length());
            }
        });

        //新密码
        cbPwd.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (!isChecked) {
                etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                etPassword.setSelection(etPassword.getText().length());
            } else {
                etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                etPassword.setSelection(etPassword.getText().length());
            }
        });
        //确认新密码
        cbPwdConfirm.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (!isChecked) {
                etPasswordConfirm.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                etPasswordConfirm.setSelection(etPasswordConfirm.getText().length());
            } else {
                etPasswordConfirm.setTransformationMethod(PasswordTransformationMethod.getInstance());
                etPasswordConfirm.setSelection(etPasswordConfirm.getText().length());
            }
        });

        tvRegister.setOnClickListener(listener);
    }


    private PerfectClickListener listener = new PerfectClickListener() {
        @Override
        protected void onNoDoubleClick(View v) {
            int id = v.getId();
           if (id == R.id.tv_register) {
                //修改
               String cbPwdOldStr=etPasswordOld.getText().toString().trim();

               String etPasswordStr=etPassword.getText().toString().trim();
               String etPasswordConfirmStr=etPasswordConfirm.getText().toString().trim();

               if(cbPwdOldStr==null||cbPwdOldStr.length()==0){
                   ToastUtils.showShortToast("请输入旧密码");
                   return;
               }
               if(etPasswordStr==null||etPasswordStr.length()==0){
                   ToastUtils.showShortToast("请输入新密码");
                   return;
               }

               if(etPasswordConfirmStr==null||etPasswordConfirmStr.length()==0){
                   ToastUtils.showShortToast("请再次输入新密码");
                   return;
               }
               if(!etPasswordConfirmStr.equals(etPasswordStr)){
                   ToastUtils.showShortToast("两次输入的密码不相同");
                   return;
               }
                    update(cbPwdOldStr,etPasswordStr,etPasswordConfirmStr);
            }
        }

    };


    /**
     * 修改新密码
     */
    private void update(String old,String str1,String str2) {
        registCall = RetrofitHelper.getInstance().updatepassword(userid,old, str1, str2);
        registCall.enqueue(new Callback<RegistEntity>() {
            @Override
            public void onResponse(Call<RegistEntity> call, Response<RegistEntity> response) {
                if (response != null) {
                    RegistEntity body = response.body();
                    if (body != null) {
                        if ("100".equals(body.getCode())) {
                            ToastUtils.showShortToastSafe(body.getRinfo());
                            startActivity(new Intent(EditPasswordActivity.this, LoginActivity.class));
                            finish();
                        } else {
                            ToastUtils.showShortToastSafe(body.getRinfo());
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<RegistEntity> call, Throwable t) {
                Log.e("xuke", t.getMessage());
            }
        });

    }





}


